package com.example.myapplication.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.activity.SearchActivity;
import com.example.myapplication.adapter.recyclerViewAdapter.RecommendAdapter;

public class RecommendFragment extends Fragment {
    private RecyclerView recycler_vertical;
    private View view;
    private RecyclerView recycler_hor;
    private SearchView searchView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_recommend, container, false);
        init();
        return view;
    }

    private void init() {
        searchView = view.findViewById(R.id.searchView);
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (TextUtils.isEmpty(query)) {
                    Toast.makeText(getContext(), "请输入查找内容！", Toast.LENGTH_SHORT).show();
                }
                else{
                    Intent intent = new Intent(getContext(), SearchActivity.class);
                    intent.putExtra("query",query);
                    startActivity(intent);
                }
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        });
        View mSearchPlate = view.findViewById(R.id.search_plate);
        mSearchPlate.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.white));


        recycler_vertical = view.findViewById(R.id.recycler_vertical);
        RecyclerView.LayoutManager manager_ver = new LinearLayoutManager(getContext());
        recycler_vertical.setLayoutManager(manager_ver);
        FragmentManager fragmentManager = getChildFragmentManager();
        RecommendAdapter verAdapter = new RecommendAdapter(getContext(),fragmentManager);
        recycler_vertical.setAdapter(verAdapter);
        recycler_vertical.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(dy>0){
                    searchView.setVisibility(View.INVISIBLE);
                }
                else{
                    searchView.setVisibility(View.VISIBLE);
                }
            }
        });


    }
}
